#! /usr/bin/env bash

build_playform iOS os
    build_playform iOS sim



if [ "$2" = "os" ]; then
            echo "build for ios device"
            arch="arm64"
            version=`xcodebuild -version|awk 'NR== 1 {print $2}'`
            echo "xcode version:$version"
            if [ $version \< "14" ]; then
                echo "version less than 14"
                arch="armv7 arm64"
            fi
            cmake .. -DCMAKE_TOOLCHAIN_FILE=tools/cmake/ios.toolchain.cmake -G Xcode -DPLATFORM=OS -DCMAKE_SYSTEM_NAME=iOS -DDEPLOYMENT_TARGET=9 -DARCHS="$arch" -DCMAKE_BUILD_TYPE="$CONFIG"
        elif [ "$2" = "sim" ]; then
            echo "build for ios simulator"
            cmake .. -DCMAKE_TOOLCHAIN_FILE=tools/cmake/ios.toolchain.cmake -G Xcode -DPLATFORM=SIMULATOR64 -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_BUILD_TYPE="$CONFIG"
        fi



        FMK_NAME='mydecoder'
    DEVICE_DIR=${PROJECT_DIR}/output/ios/os/${FMK_NAME}.framework
    SIMULATOR_DIR=${PROJECT_DIR}/output/ios/sim/${FMK_NAME}.framework
    INSTALL_DIR=${PROJECT_DIR}/output/ios/fat/${FMK_NAME}.framework
    echo "merge ios device and simulator framework"
    if [ -d "${INSTALL_DIR}" ]; then
        rm -rf ${INSTALL_DIR}
    fi

    mkdir -p ${PROJECT_DIR}/output/ios/fat/
    cp -LR ${DEVICE_DIR} ${INSTALL_DIR}
    lipo -create ${DEVICE_DIR}/${FMK_NAME} ${SIMULATOR_DIR}/${FMK_NAME} -output ${INSTALL_DIR}/${FMK_NAME}